// pages/setting/user/index.js
const app = getApp()
import {
  Request
} from '../../../utils/request'
import {
  uploadImgs,
  getUserByOpenId
} from '../.././../utils/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      avatar: "",
      nickname: "",
      direction: 'LEFT',
      sex: "MALE",
      tip: "遇见你是我最好的选择"
    }
  },
  async submit() {
    const http = new Request()
    const formData = this.data.formData
    const openId = wx.getStorageSync('openId')
    let res;
    let userInfo;
    if (formData._id) {
      res = await http.update("user", formData)
    } else {
      res = await http.create("user", formData)
    }
    const resp = await http.all('user')
    wx.setStorageSync('userList', resp.data)
    userInfo = getUserByOpenId(resp.data, openId)
    if (userInfo) {
      app.globalData.userInfo = userInfo
      wx.setStorageSync('userInfo', userInfo)
    }
    wx.navigateBack()
  },
  change(e) {
    const value = e.detail.value ?? e.detail
    const key = `formData.${e.currentTarget.dataset.name}`
    this.setData({
      [key]: value
    })
  },
  async bindchooseavatar(e) {
    const tempFileUrl = e.detail.avatarUrl
    const res = await uploadImgs([tempFileUrl], this)
    this.setData({
      ['formData.avatar']: res[0]
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      this.setData({
        formData: userInfo
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})